<template>
  <div class="payment-page">
    <div class="header">
      <div class="back-btn" @click="$router.go(-1)">
        <span class="back-arrow">‹</span>
      </div>
      <h1>选择支付方式</h1>
      <div class="home-btn" @click="$router.push('/home')">
        <span class="home-icon">⌂</span>
      </div>
    </div>

    <div class="payment-content">
      <div class="amount-card">
        <div class="label">支付金额</div>
        <div class="amount">¥{{ paymentAmount }}</div>
      </div>

      <div class="address-card" @click="() => {
        console.log('点击地址卡片');
        showMap = true;
        console.log('showMap:', showMap);
      }">
        <div class="address-icon">📍</div>
        <div class="address-content">
          <div class="address-title">收货地址</div>
          <div class="address-detail">{{ selectedAddress || '请选择收货地址' }}</div>
        </div>
        <div class="address-arrow">›</div>
      </div>

      <div class="payment-methods">
        <div class="method-item" 
          v-for="method in paymentMethods" 
          :key="method.id"
          :class="{ active: selectedMethod === method.id }"
          @click="selectPaymentMethod(method)">
          <div class="method-icon" :class="method.type">{{ method.icon }}</div>
          <div class="method-info">
            <div class="method-name">{{ method.name }}</div>
            <div class="method-desc" v-if="method.desc">{{ method.desc }}</div>
          </div>
          <div class="method-check">○</div>
        </div>
      </div>
    </div>

    <div class="bottom-button">
      <button class="confirm-btn" 
        :disabled="!canConfirm" 
        :class="{ disabled: !canConfirm }"
        @click="confirmPayment">
        确定支付
      </button>
    </div>

    <!-- 将地图组件移到这里 -->
    <AddressMap
      v-model:visible="showMap"
      @select-address="(address) => {
        console.log('选择地址:', address);
        handleAddressSelect(address);
      }"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import AddressMap from '../../components/AddressMap.vue'

const router = useRouter()
const route = useRoute()
const showMap = ref(false)
const selectedAddress = ref('')
const paymentAmount = ref(0)
const selectedMethod = ref('')

// 监听showMap变化
watch(() => showMap.value, (newVal) => {
  console.log('showMap changed:', newVal)
})

// 支付方式列表
const paymentMethods = [
  { id: 'alipay', name: '支付宝', icon: '₳', type: 'alipay' },
  { id: 'wechat', name: '微信支付', icon: 'WeChat', type: 'wechat' },
  { id: 'card', name: '银行卡', icon: '💳', type: 'card', desc: '**** **** **** 2109' }
]

// 选择支付方式
const selectPaymentMethod = (method: any) => {
  selectedMethod.value = method.id
}

// 确认按钮是否可点击
const canConfirm = computed(() => {
  return selectedMethod.value && selectedAddress.value
})

// 处理地址选择
const handleAddressSelect = (address: any) => {
  console.log('收到选择的地址:', address) // 添加日志
  selectedAddress.value = address.address
  localStorage.setItem('deliveryAddress', JSON.stringify(address))
}

// 确认支付
const confirmPayment = () => {
  if (!canConfirm.value) return
  
  // TODO: 处理支付逻辑
  router.push('/payment-success')
}

// 组件加载时读取保存的地址
onMounted(() => {
  const savedAddress = localStorage.getItem('deliveryAddress')
  if (savedAddress) {
    const address = JSON.parse(savedAddress)
    selectedAddress.value = address.address
  }

  // 优先从路由参数获取支付金额
  if (route.query.amount) {
    paymentAmount.value = parseFloat(route.query.amount as string)
    // 保存到localStorage以备后用
    localStorage.setItem('paymentAmount', paymentAmount.value.toString())
  } else {
    // 如果路由参数没有，则从localStorage获取
    const amount = localStorage.getItem('paymentAmount')
    if (amount) {
      paymentAmount.value = parseFloat(amount)
    }
  }

  // 如果金额为0或无效，返回上一页
  if (!paymentAmount.value || isNaN(paymentAmount.value)) {
    console.error('无效的支付金额')
    router.go(-1)
  }
})
</script>

<style scoped>
.payment-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 80px;
}

.header {
  display: flex;
  align-items: center;
  padding: 16px;
  background: #1890ff;
  color: white;
}

.back-btn, .home-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.back-arrow {
  font-size: 24px;
}

.home-icon {
  font-size: 20px;
}

.header h1 {
  flex: 1;
  margin: 0;
  text-align: center;
  font-size: 18px;
}

.payment-content {
  padding: 16px;
}

.amount-card {
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 16px;
  text-align: center;
}

.label {
  color: #666;
  font-size: 14px;
  margin-bottom: 8px;
}

.amount {
  color: #1890ff;
  font-size: 32px;
  font-weight: bold;
}

.address-card {
  background: white;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.address-icon {
  font-size: 24px;
  margin-right: 12px;
}

.address-content {
  flex: 1;
}

.address-title {
  font-size: 14px;
  color: #666;
  margin-bottom: 4px;
}

.address-detail {
  font-size: 16px;
  color: #333;
}

.address-arrow {
  font-size: 20px;
  color: #999;
}

.payment-methods {
  background: white;
  border-radius: 8px;
  overflow: hidden;
}

.method-item {
  display: flex;
  align-items: center;
  padding: 16px;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
}

.method-item:last-child {
  border-bottom: none;
}

.method-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  font-size: 24px;
}

.method-icon.alipay {
  background: #1677ff;
  color: white;
}

.method-icon.wechat {
  background: #07c160;
  color: white;
}

.method-icon.card {
  background: #f5f5f5;
  color: #666;
}

.method-info {
  flex: 1;
}

.method-name {
  font-size: 16px;
  color: #333;
  margin-bottom: 2px;
}

.method-desc {
  font-size: 12px;
  color: #999;
}

.method-check {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid #ddd;
  display: flex;
  align-items: center;
  justify-content: center;
  color: transparent;
  margin-left: 12px;
}

.method-item.active .method-check {
  border-color: #1890ff;
  background: #1890ff;
  color: white;
}

.bottom-button {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  background: white;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
}

.confirm-btn {
  width: 100%;
  height: 44px;
  border: none;
  border-radius: 22px;
  background: #1890ff;
  color: white;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
}

.confirm-btn.disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 添加地图相关样式 */
.payment-page {
  position: relative;
}

:deep(.map-modal) {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
}
</style> 